package com.li.pm.function;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


import android.app.AlertDialog;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.Camera;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.IPowerManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.WindowManager;

public class StateManagerFunction {

	private Context context;
	private final WifiManager mWifiManager;
	private ContentResolver mResolver;
	private ConnectivityManager mConnManager;
	private final BluetoothAdapter btAdapt;
	private final AudioManager mAudioManager;

	private static StateManagerFunction mService;

	private StateManagerFunction(Context c) {
		context = c;
		mWifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		mResolver = context.getContentResolver();
		mConnManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		btAdapt = BluetoothAdapter.getDefaultAdapter();
		mAudioManager = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);

	}

	public static StateManagerFunction getInstance(Context c) {
		if (mService != null) {
			return mService;
		} else {
			mService = new StateManagerFunction(c);
			return mService;
		}
	}

	/**
	 * wifi 模块
	 * 
	 * @return
	 */
	public boolean getWifiState() {
		return mWifiManager.isWifiEnabled();
	}

	public boolean setWifiEnabled(boolean b) {
		return mWifiManager.setWifiEnabled(b);
	}

	/**
	 * apn 模块
	 * 
	 * @return
	 */
	public boolean getPPPState() {
		try {
			return getMobileDataEnabled();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public boolean setPPPEnabled(boolean b) {
		try {
			setMobileDataEnabled(b);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 蓝牙模块
	 * 
	 * @return
	 */
	public boolean getBTState() {
		try {
			return btAdapt.isEnabled();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public boolean setBTEnabled(boolean b) {
		try {
			if (b)
				btAdapt.enable();
			else
				btAdapt.disable();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 重力感应模块
	 * 
	 * @return
	 */
	public boolean getAccelerometerState() {

		return Settings.System.getInt(context.getContentResolver(),
				Settings.System.ACCELEROMETER_ROTATION, 0) != 0;

	}

	public boolean setAccelerometerEnabled(boolean b) {
		try {
			if (b)
				Settings.System.putInt(context.getContentResolver(),
						Settings.System.ACCELEROMETER_ROTATION, 1);
			else
				Settings.System.putInt(context.getContentResolver(),
						Settings.System.ACCELEROMETER_ROTATION, 0);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 同步 模块
	 * 
	 * @return
	 */
	public boolean getSyncState() {
		return ContentResolver.getMasterSyncAutomatically();

	}

	public void setSyncEnabled(boolean b) {
		ContentResolver.setMasterSyncAutomatically(b);
	}

	/**
	 * 声音 模块
	 * 
	 * @return
	 */
	public int getSoundState() {
		return mAudioManager.getRingerMode();

	}

	public void setSoundState(int ringerMode) {
		mAudioManager.setRingerMode(ringerMode);
	}

	/**
	 * 亮度 模块
	 * 
	 * @return
	 */
	public int getBrightnessMode(int defaultValue) {
		int brightnessMode = defaultValue;
		try {
			brightnessMode = Settings.System.getInt(
					context.getContentResolver(),
					Settings.System.SCREEN_BRIGHTNESS_MODE);
		} catch (SettingNotFoundException snfe) {
		}
		return brightnessMode;
	}

	public void setBrightnessMode(int mode) {
		Settings.System.putInt(context.getContentResolver(),
				Settings.System.SCREEN_BRIGHTNESS_MODE, mode);
	}

	public void setBrightness(int brightness) {

		 Settings.System.putInt(context.getContentResolver(),  Settings.System.SCREEN_BRIGHTNESS,brightness);
		
	}

	public int getBrightness(int defaultValue) {
		int brightness = defaultValue;
		try {
			brightness = Settings.System.getInt(context.getContentResolver(),
					Settings.System.SCREEN_BRIGHTNESS);
		} catch (SettingNotFoundException snfe) {
		}
		return brightness;
	}

	/**
	 * 进入setting 模块
	 * 
	 * @return
	 */
	public void startSettings() {
		Intent intent = new Intent("android.settings.SETTINGS");
		context.startActivity(intent);
	}

	/*************************************************************************************************************************/
	/****************************************************** 私有方法 

************************************************************/
	/*************************************************************************************************************************/

	/**
	 * 设置数据使用状态 需要 签名 系统 可以根据 变化
	 * 
	 * @param b
	 *            true 为可用 false为 不可用
	 * @return 0为 成功 -1为失败
	 * @throws NoSuchMethodException
	 * @throws SecurityException
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	private void setMobileDataEnabled(boolean b) throws SecurityException,
			NoSuchMethodException, IllegalArgumentException,
			IllegalAccessException, InvocationTargetException {
		Method setMobileDataEnabl;
		setMobileDataEnabl = mConnManager.getClass().getDeclaredMethod(
				"setMobileDataEnabled", boolean.class);
		// setMobileDataEnabl.setAccessible(true);
		setMobileDataEnabl.invoke(mConnManager, b);
	}

	/**
	 * 获得当前 数据使用状态 不需要 签名
	 * 
	 * @return 0 数据开关 打开 1 数据开关 关闭 2 其他 报错 等等。
	 * @throws NoSuchMethodException
	 * @throws SecurityException
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	private boolean getMobileDataEnabled() throws SecurityException,
			NoSuchMethodException, IllegalArgumentException,
			IllegalAccessException, InvocationTargetException {
		boolean result = false;

		Method setMobileDataEnabl;
		setMobileDataEnabl = mConnManager.getClass().getDeclaredMethod(
				"getMobileDataEnabled");
		// setMobileDataEnabl.setAccessible(true);
		result = ((Boolean) setMobileDataEnabl.invoke(mConnManager))
				.booleanValue();
		return result;
	}

	public void reboot() {
		Intent i = new Intent(Intent.ACTION_REBOOT);
		i.putExtra("nowait", 1);
		i.putExtra("interval", 1);
		i.putExtra("window", 0);
		context.sendBroadcast(i);
	}

	public void shutdown() {
		Intent newIntent = new Intent(
				"android.intent.action.ACTION_REQUEST_SHUTDOWN");
		newIntent.putExtra("android.intent.extra.KEY_CONFIRM", false);
		newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(newIntent);
	}

}
